package com.spark.colliery

import android.Manifest
import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.spark.common.BaseFragment
import com.spark.colliery.common.ext.gone
import com.spark.colliery.module.agency.AgencyHomeFragment
import com.spark.colliery.module.home.HomeFragment
import com.spark.colliery.module.home.fragment.HomeCommonFragment
import com.spark.colliery.module.learn.LearnHomeFragment
import com.spark.colliery.module.message.MessageHomeFragment
import com.spark.colliery.module.mine.MineFragment
import com.tbruyelle.rxpermissions2.RxPermissions
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.android.synthetic.main.act_main.*
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.item.NormalItemView
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener
import me.yokeyword.fragmentation.SupportFragment

@Suppress("DEPRECATION")
/**
 * Main Fragment
 */
class MainFragment : BaseFragment() {
    private lateinit var moduleAdapter: ModuleAdapter
    private var currPos = 0

    companion object {
        fun newInstance(): MainFragment {
            val fragment = MainFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_main
    }

    @SuppressLint("ShowToast", "CheckResult")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val navTabs = tab.custom()
                .addItem(commonTab(R.drawable.tab_home, R.drawable.tab_home_selected, "首页"))
                .addItem(commonTab(R.drawable.tab_learn, R.drawable.tab_learn_selected, "学习"))
                .addItem(commonTab(R.drawable.tab_agency, R.drawable.tab_agency_selected, "待办"))
                .addItem(commonTab(R.drawable.tab_message, R.drawable.tab_message_selected, "消息"))
                .addItem(commonTab(R.drawable.tab_mine, R.drawable.tab_mine_selected, "我的"))
                .build()
        pager.apply {
            moduleAdapter = ModuleAdapter(childFragmentManager, 5)
            adapter = moduleAdapter
            canScroll = false
            navTabs.also {
                it.setupWithViewPager(this)
                it.addTabItemSelectedListener(object : OnTabItemSelectedListener {
                    override fun onSelected(index: Int, old: Int) {
                        currPos = index
                    }

                    override fun onRepeat(index: Int) {
                    }

                })
            }
        }
    }

    private fun commonTab(normalIcon: Int, checkedIcon: Int, text: String): BaseTabItem {
        return NormalItemView(context).apply {
            setBackgroundColor(resources.getColor(R.color.white))
            initialize(normalIcon, checkedIcon, text)
            setTextCheckedColor(resources.getColor(R.color.tab_selected_color))
            setTextDefaultColor(resources.getColor(R.color.tab_normal_color))
        }
    }

    fun updateTabRelation(gone: Boolean) {
        LogUtils.e(gone)
        if (currPos == 1) {
            tab.gone(gone)
        }
    }

    fun canGoback():Boolean {
        val fragment = moduleAdapter.getItem(pager.currentItem)
        if (fragment is LearnHomeFragment){
            return fragment.canGoback()
        }
        return false
    }
    /* fun checkIsHome(): Boolean {
         if (homeFragment == null) return false
         homeFragment?.apply {
             if (!checkBack()) {
                 pop()
                 return true
             }
         }
         return false
     }*/

    fun starz(target: SupportFragment) {
        start(target)
    }
}

/**
 * Module
 */
class ModuleAdapter(fm: FragmentManager, var size: Int) : FragmentPagerAdapter(fm) {
    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 -> HomeFragment.newInstance()
            1 -> LearnHomeFragment.newInstance()
            2 -> AgencyHomeFragment.newInstance()
            3 -> MessageHomeFragment.newInstance()
            else -> MineFragment.newInstance()
        }
    }

    override fun getCount(): Int {
        return size
    }

}